package com.liuwei.spring.cloud.junit.leetcode.interview;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class RotateArray {

    /**
     * 给定一个整数数组 nums，将数组中的元素向右轮转 k 个位置，其中 k 是非负数
     * @param args
     */
    public static void main(String[] args) {
        RotateArray solution = new RotateArray();
        int[] array = new int[]{1,2,3,4,5,6,7};
        solution.rotate(array,-1);

        List<Integer> values = Arrays.asList(1,2,3,4,5);
        Arrays.stream(array).forEach(System.out::println);
        String splits = Arrays.stream(array).mapToObj(String::valueOf).collect(Collectors.joining(","));
        System.out.println(splits);
        System.out.println(String.join(",",Arrays.toString(array)));
        System.out.println(Arrays.toString(values.toArray()));
    }

    public void rotate(int[] nums, int k) {
        int[] result = new int[nums.length];
        int startIndex = nums.length-(k%nums.length);
        for (int i = 0; i < nums.length; i++) {
             int value = nums[(startIndex+i)%nums.length];
            result[i] = value;
        }
        System.out.println(Arrays.toString(result));
    }
}
